package com.maas.util.mybatis.jpa.littlenb.support.template.delete;

import com.littlenb.mybatisjpa.support.template.SqlTemplate;
import com.littlenb.mybatisjpa.util.PersistentUtil;
import org.apache.ibatis.jdbc.SQL;

import javax.persistence.Column;
import java.lang.reflect.Field;

/**
 * @author sway.li
 **/
public class DeleteSqlTemplate implements SqlTemplate {

  public static final SqlTemplate INSTANCE = new DeleteSqlTemplate();



  public static boolean selectable(Field field) {
    if (!PersistentUtil.isPersistentField(field) || PersistentUtil.isAssociationField(field)) {
      return false;
    }

    if (field.isAnnotationPresent(Column.class)) {
      Column column = field.getAnnotation(Column.class);
      return column.insertable();
    }

    return true;
  }

  @Override
  public String parseSQL(final Class<?> type) {
    return new SQL() {
      {
        String table =PersistentUtil.getTableName(type);
        DELETE_FROM(table);
      }
    }.toString();
  }

}
